# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver=1.1.1m
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!buildflags' 'staticlibs')
license=('custom:BSD')
url="https://www.openssl.org"
source=(https://www.openssl.org/source/${_realname}-${_ver}.tar.gz{,.asc}
        'pathtools.c'
        'pathtools.h'
        'openssl-1.1.1-relocation.patch'
        'openssl-1.1.1-mingw-arm.patch')
sha256sums=('f89199be8b23ca45fc7cb9f1d8d3ee67312318286ad030f5316aca6462db6c96'
            'SKIP'
            '6f1016e6647b6340fdceefaf24ff391f4c0ea3c785ddf70c9794ca2356797888'
            '6ce4dcf4ef6c4bce48dbcb6f1b5226baf79f74ac76719fb0c06419a0aadb37a3'
            'ca847be6a50c30db96a3323d51e09691c3d302f7d85f3eb61fabe1f08c223659'
            'd41fad88631e7b8d2a56662f2166ea97ecbc6369f2ad3eac415182bc9ac9f308')

validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491'
              '7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd ${srcdir}/${_realname}-${_ver}
  cp -fHv "${srcdir}"/pathtools.c crypto/
  cp -fHv "${srcdir}"/pathtools.h ./

  apply_patch_with_msg \
     openssl-1.1.1-relocation.patch \
     openssl-1.1.1-mingw-arm.patch
}

build() {
  rm -rf ${srcdir}/build-${MSYSTEM}

  # No support for out-of-source builds
  mkdir -p ${srcdir}/build-${MSYSTEM}
#  cp -a ${srcdir}/${_realname}-${_ver}/* ${srcdir}/build-${MSYSTEM}

  # Use mingw cflags instead of hardcoded ones
  sed -i -e '/^"mingw"/ s/-fomit-frame-pointer -O3 -Wall/-O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4/' \
     ${srcdir}/${_realname}-${_ver}/Configure
#     ${srcdir}/build-${MSYSTEM}/Configure

  case "${CARCH}" in
    i?86)
      _mingw=mingw
      ;;
    x86_64)
      _mingw=mingw64
      ;;
    armv7)
      _mingw="mingwarm32 no-asm"
      ;;
    aarch64)
      _mingw="mingwarm64 no-asm"
      ;;
  esac

  export CC=${MINGW_PREFIX}/bin/cc.exe
  export CXX=${MINGW_PREFIX}/bin/c++.exe

  cd "${srcdir}/build-${MSYSTEM}"
  export MSYS2_ARG_CONV_EXCL="--prefix="
  ${srcdir}/${_realname}-${_ver}/Configure \
    --prefix=${MINGW_PREFIX} \
    --openssldir=ssl \
    ${_mingw} \
    shared \
    zlib-dynamic \
    enable-camellia \
    enable-capieng \
    enable-idea \
    enable-mdc2 \
    enable-rc5  \
    enable-rfc3779 \
    -D__MINGW_USE_VC2005_COMPAT \
    -DOPENSSLBIN=\"\\\"${MINGW_PREFIX}/bin\\\"\"

  make ZLIB_INCLUDE=-I"${MINGW_PREFIX}"/include depend all
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make VERBOSE=1 test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/bin
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/include/openssl
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}

  make -j1 DESTDIR="${pkgdir}" MANDIR="${MINGW_PREFIX}/share/man" MANSUFFIX=ssl install
  install -D -m644 "${srcdir}/${_realname}-${_ver}/LICENSE" \
         "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/bin
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1

  install -m644 "${srcdir}/${_realname}-${_ver}/ms/applink.c" "${pkgdir}${MINGW_PREFIX}"/include/openssl/
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/c_rehash
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{ssl/misc,ssl/man}
}
